﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PokerClient.VisualDisp
{
	public partial class Seat : UserControl
	{

		delegate void updateCard1(String type);
		delegate void updateCard2(String type);
		delegate void grayThings(Boolean type);
		String chips,seat,table;
		String username;
		Client myClient;
		Table myTable;
		Boolean isPLaying;
		public Seat()
		{
			InitializeComponent();
			isPLaying = false;
			this.grayout();
			
			//this.init();
			//this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
		//	this.BackColor = Color.Transparent;
		}
		public Seat(String username, String chips, Client myClient,Table myTable) : this()
		{
			this.myClient = myClient;
			this.chips = chips;
			this.username = username;
			this.myTable = myTable;
			init();
			isPLaying = true;
			this.grayin();
		}

		private void init()
		{
			this.raisebar1.updateMax(chips);
			this.seat = myClient.sSeat;
			this.table = myClient.sTable;
		}
		public String user
		{
			get
			{
				return this.username;
			}
			set
			{
				this.username = value;
			}
		}
		public String chip
		{
			get
			{
				return this.chips;
			}
			set
			{
				this.chips = value;
				this.raisebar1.updateMax(value);
				
			}
		}

		public void grayout()
		{

			this.grayRaise(false);
			this.grayCall(false);
			this.grayCheck(false);
			this.grayFold(false);

		}
		public Client client
		{
			set
			{
				this.myClient = value;
			}
		}
		public Table sTable
		{
			set
			{
				this.myTable = value;
			}
		}
		
		public void grayin()
		{
			this.grayRaise(true);
			this.grayCall (true);
			this.grayCheck(true);
			this.grayFold(true);
		//	this.setCards("Cards\\H\\A.jpg","Cards\\S\\A.jpg");
		}

		private void grayRaise(Boolean val)
		{
			if (this.raiseBtn.InvokeRequired)
			{
				grayThings gt = new grayThings(grayRaise);
				this.raiseBtn.Invoke(gt,new object[]{val});
			}
			else
			{
				this.raiseBtn.Enabled = val;
			}
		}
		private void grayCall(Boolean val)
		{
			if (this.callBtn.InvokeRequired)
			{
				grayThings gt = new grayThings(grayCall);
				this.callBtn.Invoke(gt, new object[] { val });
			}
			else
			{
				this.callBtn.Enabled = val;
			}
		}
		private void grayCheck(Boolean val)
		{
			if (this.checkBtn.InvokeRequired)
			{
				grayThings gt = new grayThings(grayCheck);
				this.checkBtn.Invoke(gt, new object[] { val });
			}
			else
			{
				this.checkBtn.Enabled = val;
			}
		}
		private void grayFold(Boolean val)
		{
			if (this.foldBtn.InvokeRequired)
			{
				grayThings gt = new grayThings(grayFold);
				this.foldBtn.Invoke(gt, new object[] { val });
			}
			else
			{
				this.foldBtn.Enabled = val;
			}
		}

		public void setCards(String card1,String card2)
		{
			this.updateCardN1(card1);
			this.updateCardN2(card2);
		}

		private void updateCardN1(String card1)
		{
			
			if (card1Box.InvokeRequired)
			{
				updateCard1 uc = new updateCard1(updateCardN1);
				this.card1Box.Invoke(uc, new object[] { card1 });
			}
			else
			{
				
				String[] gencard = card1.Split(':');
				if (gencard.Length > 1)
					this.card1Box.ImageLocation = ("Cards\\" + gencard[0] + "\\" + gencard[1] + ".jpg");
				else
					this.card1Box.ImageLocation = card1;
			}
		}
		private void updateCardN2(String card1)
		{

			if (card2Box.InvokeRequired)
			{
				updateCard1 uc = new updateCard1(updateCardN2);
				this.card2Box.Invoke(uc, new object[] { card1 });
			}
			else
			{
				String[] gencard = card1.Split(':');
				if (gencard.Length > 1)
					this.card2Box.ImageLocation = ("Cards\\" + gencard[0] + "\\" + gencard[1] + ".jpg");
				else
					this.card2Box.ImageLocation = card1;
			}
		}


		public string getCardlocation 
		{ 
			get
			{
				return this.card1Box.ImageLocation;
			}
		}

		private void raiseBtn_Click(object sender, EventArgs e)
		{
			
				try
				{
					int i = Int32.Parse(raisebar1.bid);
					if (i >= 0)
					{
						myClient.sent(PokerClient.Connection.Protocols.ProtocolAction.raise(this.table, this.seat, this.username, (i + "")));
					}
				}
				catch (System.Exception ex)
				{
					Console.WriteLine(ex.Message);
				}
			
		}

		private void callBtn_Click(object sender, EventArgs e)
		{
			
				try
				{
					int i = Int32.Parse(raisebar1.bid);
					if (i >= 0)
					{
						myClient.sent(PokerClient.Connection.Protocols.ProtocolAction.call(this.table, this.seat, this.username, (i + "")));
					}
				}
				catch (System.Exception ex)
				{
					Console.WriteLine(ex.Message);
				}
			
		}

		private void foldBtn_Click(object sender, EventArgs e)
		{
			
				myClient.sent(PokerClient.Connection.Protocols.ProtocolAction.fold(this.table, this.seat, this.username));
				
			
		}

		private void checkBtn_Click(object sender, EventArgs e)
		{
			
				myClient.sent(PokerClient.Connection.Protocols.ProtocolAction.check(this.table, this.seat, this.username));

		}

		public void adjustChips(int bid)
		{
			int i = Int32.Parse(this.chips);
			if (i > 0)
			{
				i -= bid;
				if (i > 0)
				{
					this.raisebar1.updateMax(i + "");
					this.chip = i + "";
				}
				else
				{
					bid += i;
					this.raisebar1.updateBid(bid+" ");
					this.raisebar1.updateMax(bid + "");
					this.chip = 0 + "";
				}
			}
		}

		public void setCall(String bid)
		{
			this.raisebar1.updateBid(bid);
		}

		private void seatLBTN_Click(object sender, EventArgs e)
		{
			
			this.myClient.sent(PokerClient.Connection.Protocols.ProtocolLeaveTableSeat.leaveSeat(this.myClient.sTable));
			this.isPLaying = false;
			this.grayout();
		}
	}
}
